import { stringify } from 'qs';
import request from '@/utils/request';

// *****************************************************************************************
// * 用户管理
// *****************************************************************************************
export async function queryUser(params) {
  //await sleep(5000);
  return request(`/api/us/user?${stringify(params)}`);
}

export async function addUser(params) {
  return request('/api/us/user', {
    method: 'POST',
    body: params,
  });
}

export async function updateUser(params = {}) {
  return request('/api/us/user', {
    method: 'PUT',
    body: params,
  });
}

export async function resetPwd(params) {
  return request('/api/us/user/resetpwd', {
    method: 'PUT',
    body: params,
  });
}

export async function disableUser(params) {
  return request('/api/us/user/disable', {
    method: 'PUT',
    body: params,
  });
}

export async function removeUsers(params) {
  return request('/api/us/user', {
    method: 'DELETE',
    body: params,
  });
}

export async function removeUser(id) {
  return request('/api/us/user/' + id, {
    method: 'DELETE',
  });
}

// *****************************************************************************************
// * 角色管理
// *****************************************************************************************
export async function queryRole(params) {
  return request(`/api/us/role?${stringify(params)}`);
}

export async function queryUserRole(params) {
  return request(`/api/us/user/${params.userId}/role`);
}

export async function setUserRole(params) {
  return request(`/api/us/user/${params.userId}/role`, {
    method: 'PUT',
    body: params,
  });
}

export async function addRole(params) {
  return request('/api/us/role', {
    method: 'POST',
    body: params,
  });
}

export async function updateRole(params) {
  return request('/api/us/role', {
    method: 'PUT',
    body: params,
  });
}

export async function queryRight(params) {
  return request(`/api/us/right?${stringify(params)}`);
}

export async function queryRoleRight(params) {
  return request(`/api/us/role/${params.roleId}/right`);
}

export async function setRoleRight(params) {
  return request(`/api/us/role/${params.roleId}/right`, {
    method: 'PUT',
    body: params,
  });
}

// Sleep方法：异步方法中使用  调用，实现阻塞
// eslint-disable-next-line
function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
